package cn.yfxu.server.service.impl;

import cn.yfxu.server.pojo.Department;
import cn.yfxu.server.mapper.DepartmentMapper;
import cn.yfxu.server.pojo.Result;
import cn.yfxu.server.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yfxu
 * @since 2021-03-10
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartments(-1);
    }

    @Override
    public Result addDep(Department department) {
        department.setEnabled(true);
        departmentMapper.addDep(department);
        if (1== department.getResultCode()){
            return Result.success("添加成功!",department);
        }
        return Result.error("添加失败!");
    }

    @Override
    public Result delDep(Integer id) {
        Department dep=new Department();
        dep.setId(id);
        departmentMapper.delDep(dep);
        if (-2==dep.getResultCode()){
            return Result.error("该部门下还有子部门，删除失败！");
        }
        if (-1==dep.getResultCode()){
            return Result.error("该部门下还有员工，删除失败！");
        }
        if (1==dep.getResultCode()){
            return Result.success("删除成功！");
        }
        return Result.error("删除失败！");
    }
}
